home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / pine3.92 / pico / msdlg.c < prev    next >
C/C++ Source or Header  |  1996-03-14  |  25KB  |  1,036 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: msdlg.c,v 1.5 1996/03/15 07:41:11 hubert Exp $";
  3. #endif
  4. /*---------------------------------------------------------------------------
  5.  *
  6.  *  Module: msdlg.c
  7.  *
  8.  * Thomas Unger
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: tunger@cac.washington.edu
  15.  *
  16.  *
  17.  * Pine and Pico are registered trademarks of the University of Washington.
  18.  * No commercial use of these trademarks may be made without prior written
  19.  * permission of the University of Washington.
  20.  * 
  21.  * Pine, Pico, and Pilot software and its included text are Copyright
  22.  * 1989-1996 by the University of Washington.
  23.  * 
  24.  * The full text of our legal notices is contained in the file called
  25.  * CPYRIGHT, included with this distribution.
  26.  *
  27.  *--------------------------------------------------------------------------*/
  28.  
  29. #define WIN31 
  30. #define STRICT
  31.  
  32. #include <windows.h>
  33. #include <commdlg.h>
  34. #ifndef    WIN32
  35. #include <print.h>
  36. #include <toolhelp.h>
  37. #endif
  38. #include <cderr.h>
  39. #include <winsock.h>
  40. #include <shellapi.h>
  41.  
  42. #include <limits.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdarg.h>
  46. #include <errno.h>
  47. #include <signal.h>
  48. #include <setjmp.h>
  49. /*#include <conio.h>*/
  50. #include <time.h>
  51. /*#include <signal.h>*/
  52. #include <fcntl.h>
  53.  
  54. #define    termdef    1            /* don't define "term" external */
  55.  
  56. #include "osdep.h"
  57. #include "pico.h"
  58. #include "estruct.h"
  59. #include "efunc.h"
  60. #include "edef.h"
  61. #include "msmenu.h"
  62.  
  63.  
  64. /* Windows only version and resource defines. */
  65. #include "resource.h"
  66.  
  67.  
  68. extern HWND        ghTTYWnd;
  69. extern HINSTANCE    ghInstance;
  70. extern BOOL        gfUseDialogs;
  71. extern FILE        *mswin_debugfile;
  72. extern int        mswin_debug;
  73. extern char        gszAppName[45];
  74.  
  75.  
  76.  
  77. #define BTN_FIRSTID    200
  78. #define BSPACE        4
  79. #define BWIDTH_MIN    120
  80.  
  81.  
  82. typedef struct {
  83.     char    *prompt;        /* Prompt. */
  84.     char    *string;        /* Resulting string. */
  85.     int        strlen;            /* Length of buffer. */
  86.     int        append;            /* Append to existing string. */
  87.     int        passwd;            /* Passwd, don't echo. */
  88.     unsigned    flags;            /* other flags. */
  89.     int        dflt;
  90.     int        cancel;
  91.     int        button_count;        /* Number of additional buttons. */
  92.     MDlgButton    *button_list;        /* List of other buttons. */
  93.     char    **helptext;        /* Help text. */
  94.     char    helpkey;
  95.     int        result;
  96. } OEInfo;
  97.  
  98.  
  99. static OEInfo    gOEInfo;        
  100. static BOOL    gDoHelpFirst;
  101. static WNDPROC    gpOldEditProc;        /* Old Edit window proc. */
  102. static WNDPROC  gpEditProc; 
  103. static WNDPROC    gpOldBtnProc;        /* Old Edit window proc. */
  104. static WNDPROC  gpBtnProc; 
  105.  
  106.  
  107.  
  108.  
  109. /*
  110.  * Forward function declaratins.
  111.  */
  112. LONG FAR PASCAL __export   EditProc(HWND hBtn, UINT msg, WPARAM wParam, 
  113.                     LPARAM lParam);
  114. LONG FAR PASCAL __export   ButtonProc(HWND hBtn, UINT msg, WPARAM wParam, 
  115.                     LPARAM lParam);
  116. BOOL CALLBACK  __export    mswin_dialog_proc (HWND hDlg, UINT uMsg, 
  117.                     WPARAM wParam, LPARAM lParam);
  118. BOOL CALLBACK  __export    mswin_select_proc (HWND hDlg, UINT uMsg, 
  119.                     WPARAM wParam, LPARAM lParam);
  120. LONG FAR PASCAL __export   KBCProc (HWND hWnd, UINT uMsg, WPARAM wParam, 
  121.                     LPARAM lParam);
  122.  
  123.                 
  124. static void        BuildButtonList (HWND hDlg, MDlgButton *built_in, 
  125.                 int biCount, MDlgButton *button_list);
  126. static BOOL        ProcessChar (HWND hWnd, WPARAM wParam, 
  127.                 OEInfo *oeinfo, long *ret);
  128. static void        GetBtnPos (HWND hPrnt, HWND hWnd, RECT *r);
  129.             
  130.             
  131.  
  132.  
  133.             
  134.             
  135.             
  136. int
  137. mswin_usedialog (void)
  138. {
  139.     return (gfUseDialogs);
  140. }
  141.             
  142.             
  143.  
  144. /*---------------------------------------------------------------------- 
  145.  
  146.  
  147.   Generic text entry.  Prompt user for a string.
  148.       
  149.  
  150.   Args:
  151.         prompt -- The string to prompt with
  152.     string -- the buffer result is returned in, and original string (if 
  153.                    any) is passed in.
  154.         field_len -- Maximum length of string to accept
  155.         append_current -- flag indicating string should not be truncated before
  156.                           accepting input
  157.         passwd -- a pass word is being fetch. Don't echo on screen
  158.     button_list -- pointer to array of mDlgButton's.  input chars matching
  159.                        those in list return value from list.  Nearly identical
  160.                to Pine's ESCKEY structure.
  161.         help   -- Arrary of strings for help text in bottom screen lines
  162.         flags  -- flags
  163.  
  164.   Result:  editing input string
  165.             returns -1 unexpected errors
  166.             returns 0  typed CR or pressed "OK"
  167.             returns 1  typed ESC or pressed "Cancel"
  168.             returns 3  typed ^G or PF1 (help)
  169.             returns 4  typed ^L for a screen redraw
  170.        or one of the other return values defined in button_list.
  171.  
  172.   WARNING: Care is required with regard to the escape_list processing.
  173.            The passed array is terminated with an entry that has ch = -1.
  174.            Function key labels and key strokes need to be setup externally!
  175.        Traditionally, a return value of 2 is used for ^T escapes.
  176.            
  177.            
  178.   Note About Help:  We don't get the help text on the first call.  If we
  179.       want help text we have to return 3.  We'll get called again
  180.       with help text.  To make it look good, we want to display
  181.       this the second time we're called.  But we don't want to
  182.       display the help text every time we're called with help
  183.       text.  To know the difference we set gDoHelpFirst when
  184.       exiting to request help text.  If this is set then we
  185.       display help.  If not, then no help.
  186.  
  187.  
  188. ----------------------------------------------------------------------*/
  189.             
  190.             
  191. /* 
  192.  * xxx implement flags.
  193.  * xxx   display.d uses flags QFFILE, QDEFLT, and QBOBUF
  194.  */
  195.  
  196. int
  197. mswin_dialog (char *prompt, char *string, int field_len, int append_current,
  198.         int passwd, MDlgButton *button_list, char **help, unsigned flags)
  199. {
  200.     DLGPROC    dlgprc;
  201.     int        c;
  202.     
  203.     
  204.     mswin_flush ();
  205.     
  206.     
  207.     gOEInfo.prompt = prompt;
  208.     gOEInfo.string = string;
  209.     gOEInfo.strlen = field_len;
  210.     gOEInfo.append = append_current;
  211.     gOEInfo.passwd = passwd;
  212.     gOEInfo.helptext = help;
  213.     gOEInfo.helpkey = 0x07;    /* ^G */
  214.     gOEInfo.flags = flags;
  215.     gOEInfo.button_list = button_list;
  216.     gOEInfo.result = 0;
  217.  
  218.     for (c = 0; button_list && button_list[c].ch != -1; ++c);
  219.     gOEInfo.button_count = c;
  220.     
  221.     gpEditProc = (WNDPROC) MakeProcInstance ((FARPROC) EditProc, 
  222.                     ghInstance);
  223.     
  224.     gpBtnProc = (WNDPROC) MakeProcInstance ((FARPROC) ButtonProc, 
  225.                     ghInstance);
  226.     
  227.     
  228.     dlgprc = (DLGPROC) MakeProcInstance ((FARPROC) mswin_dialog_proc, 
  229.                     ghInstance);
  230.     DialogBox (ghInstance, MAKEINTRESOURCE (IDD_OPTIONALYENTER), 
  231.             ghTTYWnd, dlgprc);
  232.     FreeProcInstance((FARPROC) dlgprc);
  233.     FreeProcInstance((FARPROC) gpBtnProc);
  234.     FreeProcInstance((FARPROC) gpEditProc);
  235.     return (gOEInfo.result);
  236. }
  237.         
  238.  
  239.     
  240.     
  241. /*
  242.  * Dialog procedure for the generic text entry dialog box.
  243.  */
  244. BOOL CALLBACK  __export 
  245. mswin_dialog_proc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  246. {
  247.     BOOL    ret = FALSE;
  248.     int        i, l;
  249.     int        biCount;
  250.     HWND    hEdit;
  251.     MDlgButton    built_in[3];
  252.     
  253.     
  254.     switch (uMsg) {
  255.     case WM_INITDIALOG:
  256.     /*
  257.      * Set the prompt text.
  258.      */
  259.     SetDlgItemText (hDlg, IDC_PROMPT, gOEInfo.prompt);
  260.  
  261.     /*
  262.      * Set the initial edit text and configure the edit control
  263.      */
  264.     if (!gOEInfo.append)
  265.         *gOEInfo.string = '\0';
  266.     SetDlgItemText (hDlg, IDC_RESPONCE, gOEInfo.string);
  267.     SendDlgItemMessage (hDlg, IDC_RESPONCE, EM_SETPASSWORDCHAR,
  268.         gOEInfo.passwd ? '*' : 0, 0L);
  269.     SendDlgItemMessage (hDlg, IDC_RESPONCE, EM_LIMITTEXT, 
  270.         gOEInfo.strlen - 1, 0L);
  271.     l = lstrlen (gOEInfo.string);
  272.     SendDlgItemMessage(hDlg, IDC_RESPONCE, EM_SETSEL, 0, 
  273.         MAKELONG(l, l));
  274.     hEdit = GetDlgItem (hDlg, IDC_RESPONCE);
  275.     SetFocus (hEdit);
  276.     gpOldEditProc = (WNDPROC) GetWindowLong (hEdit, GWL_WNDPROC);
  277.     SetWindowLong (hEdit, GWL_WNDPROC, (DWORD)(FARPROC)gpEditProc);
  278.     
  279.     
  280.     /*
  281.      * Subclass the standard buttons and build buttons for each item 
  282.      * in the button_list.
  283.      */
  284.     gpOldBtnProc = (WNDPROC) GetWindowLong (GetDlgItem (hDlg, IDOK), GWL_WNDPROC);
  285.     SetWindowLong (GetDlgItem (hDlg, IDOK), GWL_WNDPROC, 
  286.                     (DWORD)(FARPROC)gpBtnProc);
  287.     SetWindowLong (GetDlgItem (hDlg, IDCANCEL), GWL_WNDPROC, 
  288.                     (DWORD)(FARPROC)gpBtnProc);
  289.  
  290.     memset (&built_in, 0, sizeof (built_in));
  291.     built_in[0].id = IDOK;
  292.     built_in[1].id = IDCANCEL;
  293.     if (1) {
  294.         built_in[2].id = IDC_GETHELP;
  295.         biCount = 3;
  296.         }
  297.     else {
  298.         DestroyWindow (GetDlgItem (hDlg, IDC_GETHELP));
  299.         biCount = 2;
  300.         }
  301.     BuildButtonList (hDlg, built_in, biCount, gOEInfo.button_list);
  302.  
  303.     
  304.     if (gOEInfo.helptext && gDoHelpFirst) {
  305.         mswin_showhelpmsg ((WINHAND)hDlg, gOEInfo.helptext);
  306.         gDoHelpFirst = FALSE;
  307.         }
  308.     return (0);
  309.     
  310.  
  311.     case WM_ACTIVATE:
  312.     /*
  313.      * Keep focus on the edit item so key strokes are always
  314.      * received.
  315.      */
  316.     SetFocus (GetDlgItem (hDlg, IDC_RESPONCE));
  317.     break;
  318.  
  319.     
  320.     case WM_COMMAND:
  321.     switch (wParam) {
  322.     case IDOK:
  323.         /*
  324.          * Normal exit.  Accept the new text. 
  325.          */
  326.         GetDlgItemText (hDlg, IDC_RESPONCE, gOEInfo.string, 
  327.             gOEInfo.strlen);
  328.         gOEInfo.result = 0;
  329.         EndDialog (hDlg, gOEInfo.result);
  330.         ret = TRUE;
  331.         break;
  332.         
  333.     case IDCANCEL:
  334.         /*
  335.          * Cancel operation.  Don't retreive new text.
  336.          */
  337.         gOEInfo.result = 1;
  338.         EndDialog (hDlg, gOEInfo.result);
  339.         ret = TRUE;
  340.         break;
  341.         
  342.     case IDC_GETHELP:
  343.         /*
  344.          * Get help.  If we have help text display it.  If not,
  345.          * return value 3, which tells the caller to provide us
  346.          * with help text.
  347.          */
  348.         if (gOEInfo.helptext != NULL) {
  349.         mswin_showhelpmsg ((WINHAND)hDlg, gOEInfo.helptext);
  350.         }
  351.         else {
  352.         GetDlgItemText (hDlg, IDC_RESPONCE, gOEInfo.string, 
  353.             gOEInfo.strlen);
  354.         gOEInfo.result = 3;
  355.         gDoHelpFirst = TRUE;
  356.         EndDialog (hDlg, gOEInfo.result);
  357.         }
  358.         ret = TRUE;
  359.         break;
  360.         
  361.     default:
  362.         /*
  363.          * Search button list for button with this ID.  If found
  364.          * return it's result code.  Retreive text.
  365.          */
  366.         if (  wParam >= BTN_FIRSTID && 
  367.           wParam < BTN_FIRSTID + gOEInfo.button_count) {
  368.         GetDlgItemText (hDlg, IDC_RESPONCE, gOEInfo.string, 
  369.             gOEInfo.strlen);
  370.         i = wParam - BTN_FIRSTID;
  371.         gOEInfo.result = gOEInfo.button_list[i].rval;
  372.         EndDialog (hDlg, gOEInfo.result);
  373.         ret = TRUE;
  374.         }
  375.     }
  376.     
  377.     }
  378.     return (ret) ;
  379. }
  380.  
  381.  
  382.  
  383.  
  384. /*
  385.  * Subclassed window procedure for Edit control on generic text
  386.  * entry dialog box.
  387.  */
  388. LONG FAR PASCAL __export
  389. EditProc (HWND hEdit, UINT msg, WPARAM wParam, LPARAM lParam)
  390. {
  391.     HWND    hDlg;
  392.     int        i;
  393.     int        id;
  394.     LONG    ret;
  395.     
  396.     hDlg = GetParent (hEdit);
  397.     
  398.     if (msg == WM_GETDLGCODE) {
  399.     /*
  400.      * Tell windows that we want to receive ALL keystrokes.
  401.      */
  402.     ret = CallWindowProc (gpOldEditProc, hEdit, msg, wParam, lParam);
  403.     ret |= DLGC_WANTALLKEYS;
  404.     return (ret);
  405.     }
  406.     
  407.  
  408.     if (msg == WM_CHAR) {
  409.     /*
  410.      * See if the character typed is a shortcut for any of the
  411.      * buttons.
  412.      */
  413.     if (ProcessChar (hDlg, wParam, &gOEInfo, &ret)) 
  414.          return (ret);
  415.         
  416.         /* No... Fall through for deault processing. */
  417.     }
  418.  
  419.     return (CallWindowProc (gpOldEditProc, hEdit, msg, wParam, lParam));
  420. }
  421.  
  422.  
  423.  
  424.  
  425. /*
  426.  * Subclass button windows.
  427.  *
  428.  * These buttons will automatically return the input focus to 
  429.  * a control with id IDC_RESPONCE.
  430.  */
  431. LONG FAR PASCAL __export
  432. ButtonProc (HWND hBtn, UINT uMsg, WPARAM wParam, LPARAM lParam)
  433. {
  434.     HWND    hDlg;
  435.     int        i;
  436.     int        id;
  437.     LONG    ret;
  438.     
  439.  
  440.     if (uMsg == WM_LBUTTONUP || uMsg == WM_MBUTTONUP || uMsg == WM_RBUTTONUP) {
  441.     /*
  442.      * On mouse button up restore input focus to IDC_RESPONCE, which
  443.      * processes keyboard input.
  444.      */
  445.     ret = CallWindowProc (gpOldBtnProc, hBtn, uMsg, wParam, lParam);
  446.     hDlg = GetParent (hBtn);
  447.     SetFocus (GetDlgItem (hDlg, IDC_RESPONCE));
  448.     return (ret);
  449.     }
  450.     
  451.     return (CallWindowProc (gpOldBtnProc, hBtn, uMsg, wParam, lParam));
  452. }
  453.  
  454.  
  455.  
  456.  
  457. /*
  458.  * Ask a yes/no question with the MessageBox procedure.
  459.  *
  460.  * Returns:
  461.  *    0    - Cancel operation.
  462.  *    1    - "Yes" was selected.
  463.  *    2    - "No" was selected.
  464.  */
  465. int
  466. mswin_yesno (char *prompt) 
  467. {
  468.     int        ret;
  469.     
  470.     mswin_flush ();
  471.     
  472.     
  473.     ret = MessageBox (ghTTYWnd, prompt, gszAppName, 
  474.         MB_APPLMODAL | MB_ICONQUESTION | MB_YESNOCANCEL);
  475.     
  476.     switch (ret) {
  477.     case IDYES:                ret = 1;    break;
  478.     case IDNO:                ret = 2;    break;
  479.     default:
  480.     case IDCANCEL:            ret = 0;    break;
  481.     }
  482.     return (ret);
  483. }
  484.     
  485.     
  486.  
  487.     
  488.     
  489.     
  490.  
  491.  
  492.  
  493.  
  494.  
  495. /*---------------------------------------------------------------------- 
  496.  
  497.  
  498.   Generic selection routine.  Display a prompt and a set of buttons for
  499.   each possible answer.
  500.       
  501.       
  502.  
  503.   Args:
  504.         prompt        -- The string to prompt with.
  505.     button_list    -- pointer to array of mDlgButton's.  input chars 
  506.                matching those in list return value from
  507.                mlist.  Nearly identical to Pine's ESCKEY
  508.                mstructure.
  509.     dflt        -- Value returned when "Enter" is pressed.
  510.         on_ctrl_C    -- Value returned to cancel dialog (ESC).
  511.         help        -- Arrary of strings for help text in bottom screen 
  512.                lines
  513.         flags        -- flags
  514.  
  515.   Result:  
  516.     dflt        -- Default option selected.
  517.     on_ctrl_C    -- Calcel operation.
  518.     or one of the other return values defined in button_list.
  519.         
  520.         
  521. Note:  To prcess keyboard in put we use a custom dialog control
  522.        which is invisible but always has the input focus.
  523.  
  524. ----------------------------------------------------------------------*/
  525.  
  526. int
  527. mswin_select (char *prompt, MDlgButton *button_list, int dflt, int on_ctrl_C, 
  528.             char **help, unsigned flags)
  529. {
  530.     WNDCLASS    wndclass;
  531.     DLGPROC    dlgprc;
  532.     WNDPROC    kbcProc;
  533.     int        c;
  534.  
  535.     
  536.     mswin_flush ();
  537.     
  538.     
  539.     /*
  540.      * Setup dialog config structure.
  541.      */
  542.     gOEInfo.prompt = prompt;
  543.     gOEInfo.string = NULL;
  544.     gOEInfo.strlen = 0;
  545.     gOEInfo.append = 0;
  546.     gOEInfo.passwd = 0;
  547.     gOEInfo.helptext = help;
  548.     gOEInfo.helpkey = 'g';
  549.     gOEInfo.dflt = dflt;
  550.     gOEInfo.cancel = on_ctrl_C;
  551.     gOEInfo.flags = flags;
  552.     gOEInfo.button_list = button_list;
  553.     gOEInfo.result = 0;
  554.  
  555.     /*
  556.      * Count the buttons.
  557.      */
  558.     for (c = 0; button_list && button_list[c].ch != -1; ++c);
  559.     gOEInfo.button_count = c;
  560.  
  561.     
  562.     /*
  563.      * Register the class for the user control which will receive keyboard
  564.      * input events.
  565.      */
  566.     kbcProc = (WNDPROC) MakeProcInstance ((FARPROC) KBCProc, ghInstance);
  567.     wndclass.style = 0;
  568.     wndclass.lpfnWndProc = kbcProc;
  569.     wndclass.cbClsExtra = 0;
  570.     wndclass.cbWndExtra = 0;
  571.     wndclass.hInstance = ghInstance;
  572.     wndclass.hIcon = NULL;
  573.     wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  574.     wndclass.hbrBackground = 0;
  575.     wndclass.lpszMenuName = NULL;
  576.     wndclass.lpszClassName = "KeyboardCapture";
  577.  
  578.     if (RegisterClass (&wndclass) == 0) {
  579.     /*
  580.      * There is no good return value to indicate an error.  
  581.      * Cancel the operation.
  582.      */
  583.     return (on_ctrl_C);
  584.     }
  585.     
  586.  
  587.  
  588.  
  589.     /*
  590.      * Make other procedure instances.
  591.      */
  592.     dlgprc = (DLGPROC) MakeProcInstance ((FARPROC) mswin_select_proc, 
  593.                     ghInstance);
  594.     gpBtnProc = (WNDPROC) MakeProcInstance ((FARPROC) ButtonProc, 
  595.                     ghInstance);
  596.  
  597.                 
  598.     /*
  599.      * Bring up the dialog box.
  600.      */
  601.     DialogBox(ghInstance, MAKEINTRESOURCE (IDD_SELECT), ghTTYWnd, 
  602.                 dlgprc);
  603.             
  604.             
  605.  
  606.     /*
  607.      * Free the proc instances and window class.
  608.      */
  609.     FreeProcInstance((FARPROC) dlgprc);
  610.     FreeProcInstance((FARPROC) gpBtnProc);
  611.     UnregisterClass ("KeyboardCapture", ghInstance);
  612.     FreeProcInstance((FARPROC) kbcProc);
  613.     return (gOEInfo.result);
  614. }
  615.         
  616.  
  617.     
  618.     
  619. /*
  620.  * Dialog procedure for the button selection dialog box.
  621.  */
  622. BOOL CALLBACK  __export 
  623. mswin_select_proc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  624. {
  625.     BOOL    ret = FALSE;
  626.     int        i, l;
  627.     int        biCount;
  628.     HWND    hEdit;
  629.     MDlgButton    built_in[3];
  630.     
  631.     if (mswin_debug >= 1)
  632.     fprintf (mswin_debugfile, "Select:  uMsg x%x (%d), wParam x%x, lParam x%lx\n",
  633.         uMsg, uMsg, wParam, lParam);
  634.     
  635.     
  636.     switch (uMsg) {
  637.     case WM_INITDIALOG:
  638.     /*
  639.      * Show the prompt.
  640.      */
  641.     SetDlgItemText (hDlg, IDC_PROMPT, gOEInfo.prompt);
  642.  
  643.     /*
  644.      * Set focus to the invisible custom control
  645.      */
  646.     SetFocus (GetDlgItem (hDlg, IDC_RESPONCE));
  647.     
  648.     /*
  649.      * Subclass the standard buttons and build buttons for each item 
  650.      * in the button_list.
  651.      */
  652.     gpOldBtnProc = (WNDPROC) GetWindowLong (GetDlgItem (hDlg, IDCANCEL), GWL_WNDPROC);
  653.     SetWindowLong (GetDlgItem (hDlg, IDC_GETHELP), GWL_WNDPROC, 
  654.                     (DWORD)(FARPROC)gpBtnProc);
  655.     SetWindowLong (GetDlgItem (hDlg, IDCANCEL), GWL_WNDPROC, 
  656.                     (DWORD)(FARPROC)gpBtnProc);
  657.  
  658.     memset (&built_in, 0, sizeof (built_in));
  659.     built_in[0].id = IDCANCEL;
  660.     if (gOEInfo.helptext != NULL) {
  661.         built_in[1].id = IDC_GETHELP;
  662.         biCount = 2;
  663.         }
  664.     else {
  665.         DestroyWindow (GetDlgItem (hDlg, IDC_GETHELP));
  666.         gOEInfo.helpkey = 0;
  667.         biCount = 1;
  668.         }
  669.     BuildButtonList (hDlg, built_in, biCount, gOEInfo.button_list);
  670.     gDoHelpFirst = FALSE;
  671.     return (0);
  672.     
  673.     
  674.     case WM_ACTIVATE:
  675.     /*
  676.      * Keep focus on the custom control item so key strokes are always
  677.      * received.
  678.      */
  679.     SetFocus (GetDlgItem (hDlg, IDC_RESPONCE));
  680.     break;
  681.  
  682.     
  683.     case WM_COMMAND:
  684.     switch (wParam) {
  685.     case IDOK:
  686.         gOEInfo.result = gOEInfo.dflt;
  687.         EndDialog (hDlg, gOEInfo.result);
  688.         ret = TRUE;
  689.         break;
  690.         
  691.     case IDCANCEL:
  692.         gOEInfo.result = gOEInfo.cancel;
  693.         EndDialog (hDlg, gOEInfo.result);
  694.         ret = TRUE;
  695.         break;
  696.  
  697.     case IDC_GETHELP:
  698.         /*
  699.          * Get help.  If we have help text display it.  If not,
  700.          * return value 3, which tells the caller to provide us
  701.          * with help text.
  702.          */
  703.         if (gOEInfo.helptext != NULL) {
  704.         mswin_showhelpmsg ((WINHAND) hDlg, gOEInfo.helptext);
  705.         }
  706.         ret = TRUE;
  707.         break;
  708.         
  709.     default:
  710.         if (  wParam >= BTN_FIRSTID && 
  711.           wParam < BTN_FIRSTID + gOEInfo.button_count) {
  712.         i = wParam - BTN_FIRSTID;
  713.         gOEInfo.result = gOEInfo.button_list[i].rval;
  714.         EndDialog (hDlg, gOEInfo.result);
  715.         ret = TRUE;
  716.         }
  717.     }
  718.     
  719.     }
  720.     return (ret) ;
  721. }
  722.  
  723.  
  724.  
  725. /*
  726.  * Window procedure for the hidden custom control.
  727.  */
  728. LONG FAR PASCAL __export
  729. KBCProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  730. {
  731.     int        i;
  732.     LONG    ret;
  733.     
  734.  
  735.     if (uMsg == WM_GETDLGCODE) {
  736.     /*
  737.      * Tell windows that we want all the character messages.
  738.      */
  739.     ret = DefWindowProc (hWnd, uMsg, wParam, lParam);
  740.     ret |= DLGC_WANTALLKEYS;
  741.     return (ret);
  742.     }
  743.  
  744.     
  745.     if (uMsg == WM_CHAR) {
  746.     /*
  747.      * See if the character typed is a shortcut for any of the
  748.      * buttons.
  749.      */
  750.     if (ProcessChar (GetParent (hWnd), wParam, &gOEInfo, &ret)) 
  751.          return (ret);
  752.  
  753.     /* Fall through for default processing. */
  754.     }
  755.     
  756.     return (DefWindowProc (hWnd, uMsg, wParam, lParam));
  757. }
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764. /*
  765.  * Check if character activates any button.  If it does, send WM_COMMAND
  766.  * to target window.
  767.  *
  768.  * Args:
  769.  *    hWnd    - Window to send message to.
  770.  *    wParam    - character typed.
  771.  *    oeinfo    - dialog config structure.  Contains button list.
  772.  *    ret    - value to return to windows.
  773.  *
  774.  * Returns:
  775.  *    TRUE    - Found a match.  Exit from Window Procedure returning
  776.  *          *ret to windows.
  777.  *
  778.  *    FALSE    - No match.  Continue with default processing of character
  779.  *          message.
  780.  *
  781.  */
  782. static BOOL
  783. ProcessChar (HWND hWnd, WPARAM wParam, OEInfo *oeinfo, long *ret)
  784. {
  785.     int        i;
  786.     int        id;
  787.     
  788.     
  789.     *ret = 0;
  790.     if (wParam == 0x0d) {
  791.     /*
  792.      * "Enter" is same as clicking on OK button.
  793.      */
  794.     PostMessage (hWnd, WM_COMMAND, IDOK,
  795.             MAKELONG (GetDlgItem (hWnd, IDOK), 1));
  796.     return (TRUE);
  797.     }
  798.     if (wParam == 0x1b || wParam == 0x03) {
  799.     /*
  800.      * Esc is same as clicking on Cancel button.
  801.      */
  802.     PostMessage (hWnd, WM_COMMAND, IDCANCEL,
  803.             MAKELONG (GetDlgItem (hWnd, IDCANCEL), 1));
  804.     return (TRUE);
  805.     }
  806.  
  807.     /*
  808.      * Any of the custom buttons respond to this key?
  809.      */
  810.     for (i = 0; i < oeinfo->button_count; ++i) {
  811.     if (wParam == oeinfo->button_list[i].ch) {
  812.         id = oeinfo->button_list[i].id;
  813.         PostMessage (hWnd, WM_COMMAND, id,
  814.             MAKELONG (GetDlgItem (hWnd, id), 1));
  815.         return (TRUE);
  816.     }
  817.     }
  818.     
  819.     /*
  820.      * Lastly, is it the help key?
  821.      */
  822.     if (oeinfo->helpkey != 0 && wParam == oeinfo->helpkey) {
  823.     id = IDC_GETHELP;
  824.     PostMessage (hWnd, WM_COMMAND, id,
  825.         MAKELONG (GetDlgItem (hWnd, id), 1));
  826.     return (TRUE);
  827.     }
  828.  
  829.     
  830.     /*
  831.      * Nothing we understand.
  832.      */
  833.     return (FALSE);
  834. }
  835.  
  836.  
  837.  
  838.  
  839.  
  840. /*
  841.  * Get a button position in the parent's coordinate space.
  842.  */
  843. static void
  844. GetBtnPos (HWND hPrnt, HWND hWnd, RECT *r)
  845. {
  846.     GetWindowRect (hWnd, r);
  847.     ScreenToClient (hPrnt, (POINT *) r);
  848.     ScreenToClient (hPrnt, (POINT *) &r->right);
  849. }
  850.  
  851.  
  852.  
  853.  
  854. /*
  855.  * Add buttons to the dialog box.
  856.  */
  857. static void
  858. BuildButtonList (HWND hDlg, MDlgButton *built_in, int biCount, 
  859.             MDlgButton *button_list)
  860. {
  861.     RECT    rDlg, rb1, rb2, r;
  862.     HDC        hdc;
  863.     MDlgButton    *pB;                /* pointer to button struct*/
  864.     HWND    hBtn, hBtn1, hBtn2;        /* handle to buttons */
  865.     int        btnCount;            /* extra button count */
  866.     int        rows, cols;            /* button rows and columns */
  867.     int        bpos;                /* button position */
  868.     int        i;
  869.     int        maxstrIdx, maxstrLen;        /* button w/ longest caption*/
  870.     DWORD    textExtent;            /* width of button caption */
  871.     int        margine;            /* left and right margines */
  872.     int        btop, bleft;            /* button position */
  873.     int        bwidth, bheight, w;        /* button size */
  874.     int        bMinWidth;            /* minimum buttonwidth */
  875.     int        bvertSpace, bhorzSpace;        /* button spacing */
  876.     int        newWHeight, delta;        /* window resizing */
  877.     char    caption[128];
  878.     HFONT    btnFont;
  879.    
  880.     
  881.     /*
  882.      * Are there any buttons to add?
  883.      */
  884.     if (button_list == NULL)
  885.     return;
  886.     maxstrIdx = 0;
  887.     for (btnCount = 0; button_list[btnCount].ch != -1; ++btnCount) {
  888.     if (lstrlen (button_list[btnCount].label) > 
  889.         lstrlen (button_list[maxstrIdx].label)) 
  890.         maxstrIdx = btnCount;
  891.     }
  892.     if (btnCount == 0)
  893.     return;
  894.  
  895.  
  896.     /* 
  897.      * Get the size of the dialog box and the positions of the
  898.      * first and, if there is one, the second button.  Calculate or
  899.      * default button offsets and positions.
  900.      */
  901.     GetClientRect (hDlg, &rDlg);
  902.     hBtn1 = GetDlgItem (hDlg, built_in[0].id);
  903.     GetBtnPos (hDlg, hBtn1, &rb1);
  904.     margine = rb1.left;                /* left and right margine */
  905.     bheight = rb1.bottom - rb1.top;        /* button width */
  906.     bwidth  = rb1.right - rb1.left;        /* button height. */
  907.     if (biCount > 1) {
  908.     hBtn2 = GetDlgItem (hDlg, built_in[1].id);
  909.     GetBtnPos (hDlg, hBtn2, &rb2);
  910.     bvertSpace = rb2.top - rb1.top;        /* vertical spacing */
  911.     }
  912.     else {
  913.     bvertSpace = bheight + BSPACE;        /* vertical spacing */
  914.     }
  915.     
  916.     
  917.     /*
  918.      * Get the button font.
  919.      */
  920.     btnFont = (HFONT) SendMessage (hBtn1, WM_GETFONT, 0, 0);
  921.     
  922.  
  923.     
  924.     /*
  925.      * Get the screen extent of the longest button label.  min width
  926.      * is the extent, plus the average width of 5 extra characters for
  927.      * key stroke, plus margine.
  928.      */
  929.     hdc = GetDC (hBtn1);
  930.     wsprintf (caption, "%s '%s'", button_list[maxstrIdx].label, 
  931.                   button_list[maxstrIdx].name);
  932.     maxstrLen = lstrlen (caption);
  933. #ifdef    WIN32
  934.     {
  935.     SIZE size;
  936.  
  937.     GetTextExtentPoint32 (hdc, caption, maxstrLen, &size);
  938.     textExtent = size.cx;
  939.     }
  940. #else
  941.     textExtent = GetTextExtent (hdc, caption, maxstrLen);
  942.     textExtent = LOWORD (textExtent);
  943. #endif
  944.     ReleaseDC (hBtn1, hdc);
  945.     bMinWidth = LOWORD (textExtent) + (2 * (LOWORD(textExtent) / maxstrLen));
  946.     if (bwidth < bMinWidth)
  947.     bwidth = bMinWidth;
  948.  
  949.     
  950.     
  951.     /*
  952.      * Calculate button positions.  If the buttons are going to extend
  953.      * past the right edge of the dialog box, shrink their width.  But
  954.      * if they get narrower than the min width calculated above then
  955.      * increase the number of rows.
  956.      */
  957.     rows = 1;
  958.     do {
  959.     ++rows;                    /* More rows. */
  960.     w = bwidth;                /* Original button width. */
  961.     cols = 1 + ((biCount + btnCount) - 1) / rows;    /* Calc num cols. */
  962.  
  963.     /* Need to srink button width? */
  964.     if (2 * margine + bwidth * cols + BSPACE * (cols - 1) > rDlg.right) {
  965.         w = ((rDlg.right - (2 * margine)) - (BSPACE * (cols-1))) / cols;
  966.     }
  967.     /* Did buttons get too narrow? */
  968.     } while (w < bMinWidth && cols > 1);
  969.  
  970.     bwidth = w;
  971.     bhorzSpace = bwidth + BSPACE;        /* horizontal spacing */
  972.     
  973.     
  974.     
  975.  
  976.  
  977.     /* 
  978.      * Resize window. 
  979.      */
  980.     newWHeight = rb1.top + (rows * bvertSpace);
  981.     delta = newWHeight - rDlg.bottom;
  982.     if (delta != 0) {
  983.     GetWindowRect (hDlg, &r);
  984.     MoveWindow (hDlg, r.left, r.top, r.right - r.left, 
  985.         (r.bottom - r.top) + delta, FALSE);
  986.     }
  987.     
  988.  
  989.  
  990.     /*
  991.      * Create new buttons.
  992.      */
  993.     for (bpos = 0; bpos < biCount + btnCount; ++bpos) {
  994.     /*
  995.      * Calculate position for this button.
  996.      */
  997.     btop  = rb1.top  + (bpos % rows) * bvertSpace;
  998.     bleft = rb1.left + (bpos / rows) * bhorzSpace;
  999.     
  1000.     if (bpos < biCount) {
  1001.         /* 
  1002.          * Resize existing buttons. 
  1003.          */
  1004.         MoveWindow (GetDlgItem (hDlg, built_in[bpos].id), 
  1005.             bleft, btop, bwidth, bheight, FALSE);
  1006.     }
  1007.     else {
  1008.         i = bpos - biCount;
  1009.         pB = &button_list[i];
  1010.  
  1011.         wsprintf (caption, "%s '%s'", pB->label, pB->name);
  1012.         hBtn = CreateWindow ("BUTTON", caption, 
  1013.             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
  1014.             bleft, btop, bwidth, bheight, 
  1015.             hDlg, NULL, ghInstance, NULL);
  1016.  
  1017.         pB->id = BTN_FIRSTID + i;
  1018. #ifdef    WIN32
  1019.         SetWindowLong (hBtn, GWL_ID, pB->id);
  1020. #else
  1021.         SetWindowWord (hBtn, GWW_ID, pB->id);
  1022. #endif
  1023.         SendMessage (hBtn, WM_SETFONT, (WPARAM)btnFont, MAKELPARAM (0, 0));
  1024.  
  1025.         /* Subclass button. */
  1026.         SetWindowLong (hBtn, GWL_WNDPROC, (DWORD)(FARPROC)gpBtnProc);
  1027.         EnableWindow (hBtn, TRUE);
  1028.     }
  1029.     }
  1030. }
  1031.     
  1032.     
  1033.     
  1034.     
  1035.         
  1036.